package dbsync4j.mapper.concrete.compare;

import java.util.Collection;
import java.util.Map;

import dbsync4j.core.behavior.DatabaseObject;
import dbsync4j.mapper.behavior.compare.DifferenceCompare;
import dbsync4j.mapper.behavior.diff.Difference;
import dbsync4j.mapper.concrete.diff.AttributeValueDifference;
import dbsync4j.mapper.concrete.diff.NullPointerDifference;

public class ConcretePrimaryKeyDifferenceCompare extends AbstractDifferenceCompare
		implements DifferenceCompare {

	public ConcretePrimaryKeyDifferenceCompare(DatabaseObject sourcePrimaryKey, DatabaseObject destinyPrimaryKey) {
		
		super(sourcePrimaryKey, destinyPrimaryKey);
	}

	/* (non-Javadoc)
	 * @see dbsync4j.mapper.behavior.compare.DifferenceCompare#diff()
	 */
	@Override
	public Collection<Difference> diff() {
		
		String tag = null;
		/* verifica se a lista de diferen�as j� possui ao menos um item */
		if(differences.size() == 0 ) { 
			if(this.source == null && this.destiny == null) {

				return differences;

			} else if(this.source == null || this.destiny == null) {

				tag = "primarykeypair";
				differences.add(new NullPointerDifference(source, destiny, tag));

			} else {

				tag = "primarykeyattribute";
				Map<String, String> diffResult = compareObjects(tag, source, destiny);
				Collection<String> diffKeys = diffResult.keySet();

				for(String d : diffKeys) {
					String attribute = d.replace(tag.concat("."), "");
					differences.add(new AttributeValueDifference(this.source, this.destiny, attribute, tag));
				}
			}
		}
		
		return differences;
	}
	
}
